function [M] = schurcomplement(M, n)
%
% SCHURCOMPLEMENT - find the schur complement of a symmetric matrix.
% 
% If M = [A B; C D], schur complement is defined as  A-B*inv(D)*C
% The principal submatrix A is assumed to be M(1:n, 1:n)
%
% usage : [Mcompl] = schurcomplement(M, n)
% 
% M      - a symmetric matrix
% n      - number of rows and coluns in the principal submatrix A 
%          such that A = M(1:n, 1:n)
% Mcompl - schur complement of M with respect to the principal 
%          submatrix A
% 
% P. Ajith, 12.04.07
% 
% $Id: schurcomplement.m 835 2009-08-07 17:40:10Z ajith $

A = M(1:n,1:n);
B = M(1:n,n+1:end);
C = M(n+1:end,1:n);
D = M(n+1:end,n+1:end);

M = [];

% I am using the INVERSE function written by Tim Davis instead
% of matlab's INV. This function is supposed to be better than 
% INV (for whatever reasons) and is available from
% http://www.mathworks.com/matlabcentral/fileexchange/24119
%M = A-B*double(inverse(D))*C;
M = A-B*inv(D)*C;
